let requestUrl;
let typeArray = ["父菜单", "子菜单"];
/**
 * 页码
 */
let pageList = [15, 50 ,100, 200, 500];

function updateHandler() {
    // 获取选中的表格数据
    let rowData = $("#menu_list").datagrid("getSelected");

    if (rowData) {
        requestUrl = "/menu/updateById";

        $("#id").val(rowData.id);
        $("#url").textbox("setValue", rowData.url);
        $("#name").textbox("setValue", rowData.name);
        $("#iconClass").textbox("setValue", rowData.iconClass);
        $("#type").combobox("setValue", rowData.type);
        $("#parentId").combobox("setValue", rowData.parentId);
        $("#serviceId").combobox("setValue", rowData.serviceId);

        $("#menu_dialog").dialog("open");
    } else {
        $.messager.alert("系统提示", "请选择要修改的记录~", "warning");
    }
}

function deleteHandler() {
    // 获取勾选的表格数据
    let rows = $("#menu_list").datagrid("getChecked");
    let count = rows.length;

    if (count > 0) {
        $.messager.confirm("系统提示", "确认删除勾选的" + count + "条数据？", function (bool) {
            if (bool) {
                let ids = [];

                for (let i = 0; i < rows.length; i++) {
                    ids.push(rows[i].id);
                }

                ajaxPost("/menu/deleteByIds", {
                    ids: JSON.stringify(ids)
                }, function (resp) {
                    $.messager.show({
                        title: "系统消息",
                        type: "slide",
                        msg: resp.message
                    });

                    // 重新加载表格数据
                    $("#menu_list").datagrid("reload");
                    // 重新加载父菜单
                    $("#parentId").combobox("reload", {});
                }, error);
            }
        });
    } else {
        alert("请选择要删除的记录！");
    }
}

$(document).ready(function () {

    $("#service_id").combobox({
        url: "/application/selectAll",
        valueField: "id",
        textField: "name",
        width: 150,
        prompt: "-所属应用-",
        panelHeight: "auto"
    });

    $("#parent_id").combobox({
        url: "/menu/selectParent",
        valueField: "id",
        textField: "name",
        width: 150,
        required: false,
        prompt: "-父级菜单-",
        panelHeight: "auto",
        groupField: "serviceId",
        groupFormatter: function(group) {
            ajaxGet("/application/selectById", {
                id: group
            }, function (resp) {
                let data = resp.data;

                $("#_service_" + group).html(data.name);
            }, error);

            return "<span class='blue' id='_service_" + group + "'></span>";
        }
    });

    $("#_type_").combobox({
        data: getJsonData(typeArray),
        valueField: "value",
        textField: "text",
        width: 150,
        prompt: "-菜单类型-",
        panelHeight: "auto"
    });

    $("#_name_").textbox({
        width: 150,
        required: false,
        prompt: "菜单名称"
    });

    $("#search").linkbutton({
        iconCls: "icon-search"
    }).click(function() {
        let serviceId = $("#service_id").combobox("getValue");
        let parentId = $("#parent_id").combobox("getValue");
        let type = $("#_type_").combobox("getValue");
        let name = $("#_name_").textbox("getValue");

        $("#menu_list").datagrid("reload", {
            serviceId: serviceId,
            parentId: parentId,
            type: type,
            name: name
        });
    });

    $("#clear").linkbutton({
        iconCls: "icon-cancel"
    }).click(function() {
        $("#search_form").form("clear");
    });

    /******************************************************************************************************/

    $("#parentId").combobox({
        url: "/menu/selectParent",
        valueField: "id",
        textField: "name",
        width: 150,
        required: false,
        panelHeight: "auto"
    });

    $("#name").textbox({
        width: 150,
        required: true
    });

    $("#iconClass").textbox({
        width: 150,
        required: false
    });

    fetchSelect("#type", typeArray);

    fetchServiceId();

    $("#url").textbox({
        width: 380,
        required: false
    });

    /**
     * 禁止输入空格
     */
    $("#menu_form input, #menu_form select").keydown(function () {
        let event = window.event;

        if(event.keyCode === 32) {
            event.returnValue = false;
        }
    });

    $("#menu_dialog").dialog({
        title: "菜单信息",
        width: 520,
        closed: true,
        modal: true,
        buttons: [{
            iconCls: "icon-save",
            text: "保存",
            handler: function() {
                // 验证表单
                let result = $("#menu_form").form("validate");

                if (result) {
                    // 获取表单数据
                    let data = $("#menu_form").serialize();

                    ajaxPost(requestUrl, data, function (resp) {
                        $("#menu_form").form("clear");
                        $("#menu_dialog").dialog("close");

                        // 重新加载表格数据
                        $("#menu_list").datagrid("reload");
                        // 重新加载父菜单
                        $("#parentId").combobox("reload");

                        $.messager.show({
                            title: "系统消息",
                            type: "slide",
                            msg: resp.message
                        });
                    }, error);
                } else {
                    $.messager.alert("系统提示", "请输入正确的表单项~", "warning");
                }
            }
        }, {
            iconCls: "icon-cancel",
            text: "取消",
            handler: function() {
                $("#menu_form").form("clear");
                $("#menu_dialog").dialog("close");
            }
        }]
    });

    /******************************************************************************************************/

    $("#menu_list").datagrid({
        url: "/menu/selectByPage",
        height: 600,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        selectOnCheck: false,
        pagination: true,
        pageList: pageList,
        pageSize: pageList[0],
        toolbar: [{
            iconCls: "icon-add",
            text: "添加",
            handler: function() {
                requestUrl = "/menu/insert";

                $("#menu_dialog").dialog("open");
            }
        }, "-", {
            iconCls: "icon-edit",
            text: "修改",
            handler: function() {
                updateHandler();
            }
        }, "-", {
            iconCls: "icon-cancel",
            text: "删除",
            handler: function() {
                deleteHandler();
            }
        }],
        columns: [[
            {field: "ck", checkbox: true},
            {field: "id", hidden: true},
            {field: "name", title: "菜单名", width: 200, align: "center"},
            {field: "iconClass", title: "图标样式", width: 160, align: "center"},
            {field: "url", title: "请求路径", width: 400, align: "center"},
            {field: "type", title: "菜单类型", width: 120, align: "center",
                formatter: function (value) {
                    return "<div>" + typeArray[value] + "</div>";
                }
            },
            {field: "parentId", title: "父级菜单", width: 120, align: "center",
                formatter: function (value, rowData, rowIndex) {
                    if (value) {
                        ajaxGet("/menu/selectById", {
                            id: value
                        }, function (resp) {
                            let data = resp.data;

                            $("#parent_" + rowIndex).html(data.name);
                        }, error);

                        return "<div id='parent_" + rowIndex + "'></div>";
                    }

                    return "/";
                }
            },
            {field: "serviceId", title: "所属应用", width: 160, align: "center",
                formatter: function (value, rowData, rowIndex) {
                    ajaxGet("/application/selectById", {
                        id: value
                    }, function (resp) {
                        let data = resp.data;

                        $("#service_"+ rowIndex).attr({
                            "src": data.href,
                            "title": data.note
                        }).html(data.name);
                    }, error);

                    return "<a id='service_" + rowIndex + "'></a>";
                }
            }
        ]]
    });

});